package www.woniuxy.com.class112.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileTypeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
//import www.woniuxy.com.class112.dao.NoticeDetailMapper;
//import www.woniuxy.com.class112.dao.NoticeMapper;
//import www.woniuxy.com.class112.dto.NoticeDto;
//import www.woniuxy.com.class112.entity.Notice;
//import www.woniuxy.com.class112.entity.NoticeDetail;
import www.woniuxy.com.class112.dao.NoticeDetailMapper;
import www.woniuxy.com.class112.dao.NoticeMapper;
import www.woniuxy.com.class112.dto.NoticeDto;
import www.woniuxy.com.class112.entity.Notice;
import www.woniuxy.com.class112.entity.NoticeDetail;
import www.woniuxy.com.class112.entity.NoticeDetailExample;
import www.woniuxy.com.class112.param.AddNoticeParam;
import www.woniuxy.com.class112.param.PageParam;
import www.woniuxy.com.class112.service.NoticeService;
import www.woniuxy.com.class112.util.AliyunOSSUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private AliyunOSSUtil ossUtil;
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private NoticeDetailMapper noticeDetailMapper;
//    @Override
//    public PageInfo<Notice> loadAllNotice(Integer pageNum, Integer pageSize) throws Exception{
//        PageHelper.startPage(pageNum==null?1:pageNum, pageSize==null?1:pageSize);
//        List<Notice> Notices = noticeMapper.findAll();
//        PageInfo<Notice> PageInfo = new PageInfo<>(Notices);
//        return PageInfo;
//    }
    @Override
    public PageInfo<NoticeDto> loadAllNotice(PageParam param) {
        PageHelper.startPage(param.getPageIndex()==null?1:param.getPageIndex(), param.getPageSize()==null?5:param.getPageSize());
        List<NoticeDto> noticeDtos = noticeMapper.loadAllNotice(param);
    return new PageInfo<>(noticeDtos);
    }

    @Override
    public void addNotice(AddNoticeParam param) {
        Notice notice = BeanUtil.toBean(param, Notice.class);
        notice.setCreatedTime(new Date());
        notice.setStateId(0L);
        notice.setPageView(0L);
        noticeMapper.insert(notice);
        NoticeDetail nd = BeanUtil.toBean(param, NoticeDetail.class);
        nd.setNoticeId(notice.getId());
        noticeDetailMapper.insert(nd);
    }
//    @Override
//    public void addNotice(AddNoticeParam param,Long userId) throws Exception {
//        NoticeDetail nd = BeanUtil.toBean(param, NoticeDetail.class);
//        if(param.getImageUrl()!=null){
//            String type = FileTypeUtil.getType(param.getImageUrl().getInputStream());
//            if (!"jpg".equals(type)&&!"png".equals(type)){
//                throw new Exception("文件类型错误");
//            }
//            String name = param.getImageUrl().getOriginalFilename();
//            String suffix = name.substring(name.lastIndexOf("."));
//            String filename = UUID.randomUUID().toString() + suffix;
//            String imageUrl = ossUtil.uploadFile(param.getImageUrl().getInputStream(), filename, "amour520");
//            nd.setImageUrl(imageUrl);
//        }else {
//            nd.setImageUrl(null);
//        }
//        Notice notice = BeanUtil.toBean(param, Notice.class);
//        notice.setTitle(param.getTitle());
//        notice.setUnitId(param.getUnitId());
//        notice.setTypeId(param.getTypeId());
//        notice.setTargetId(param.getTargetId());
//        notice.setSourceId(param.getSourceId());
//        notice.setCreaterId(userId);
//        notice.setCreatedTime(new Date(System.currentTimeMillis()));
//        notice.setStateId(0L);
//        noticeMapper.insert(notice);
//        Long noticeId = notice.getId();
//        nd.setNoticeId(noticeId);
//        nd.setText(param.getText());
//        nd.setAbstracts(param.getAbstracts());
//        noticeDetailMapper.insert(nd);
//    }

    @Override
    public void deleteNoticeById(Long id) throws Exception{
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        if (notice==null){
            throw new RuntimeException("公告不存在");
        }
        noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delistNotice(Long id) {
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        if (notice==null){
            throw new RuntimeException("公告不存在");
        }
        if (notice.getStateId()!=0){
            throw new RuntimeException("公告已下架");
        }
        notice.setStateId(1L);
        noticeMapper.updateByPrimaryKeySelective(notice);
    }

    @Override
    public void enableNotice(Long id) {
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        if (notice==null){
            throw new RuntimeException("公告不存在");
        }
        if (notice.getStateId()!=1){
            throw new RuntimeException("公告已下架");
        }
        notice.setStateId(0L);
        noticeMapper.updateByPrimaryKeySelective(notice);
    }

    @Override
    public NoticeDetail noticedetail(Long noticeId) {
        NoticeDetailExample example = new NoticeDetailExample();
        example.createCriteria().andNoticeIdEqualTo(noticeId);
        List<NoticeDetail> noticeDetails = noticeDetailMapper.selectByExample(example);
        if (noticeDetails==null){
            throw new RuntimeException("公告不存在");
        }
        return noticeDetails.get(0);
    }


}
